package com.itbour.web.action.tg;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.itbour.com.page.Page;
import com.itbour.common.base.BaseAction;
import com.itbour.common.util.StringUtils;
import com.itbour.dto.com.UserInfoDto;
import com.itbour.dto.tg.SceneInfoDto;
import com.itbour.service.tg.SceneInfoService;
import com.opensymphony.xwork2.ActionContext;

/**
 * 场景展示 控制Action层
 * 
 * @author 李琦
 */
public class GetSceneInfoWebAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	private SceneInfoService sceneInfoService;
	Map<String, Object> mapSenceInfoResult = new HashMap<String, Object>();
	private SceneInfoDto sceneInfoDto;
	private List<SceneInfoDto> sceneInfoList;
	private String findTitleName;
	private String findSceneType;

	/**
	 * 分页获取--场景信息
	 */
	public String getSceneInfoPg() {
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto) session.get("user");
		if (user != null && user.getUser_id() != null) {
			HttpServletRequest request = ServletActionContext.getRequest();
			Integer pageNumber = Integer.parseInt(request.getParameter("page"));// 请求页码
			Integer pageSize = Integer.parseInt(request.getParameter("rows"));// 一页展示多少条
			Map<String, Object> parm = new HashMap<String, Object>();
			if (StringUtils.isNotBlank(findTitleName)) {
				parm.put("title", findTitleName);
			}
			if (StringUtils.isNotBlank(findSceneType)) {
				parm.put("type", findSceneType);
			}
			Page page = null;
			page = sceneInfoService.pageQuery(parm, pageNumber, pageSize);
			Map<String, Object> resMap = new HashMap<String, Object>();
			resMap.put("total", page.getTotalCount());// 获取总条数
			resMap.put("rows", page.getResult());// 获取页面内容
			mapSenceInfoResult = resMap;
		}
		return SUCCESS;
	}

	/***
	 * 添加场景信息
	 * 
	 * @throws Exception
	 */
	public void addSceneInfo() throws Exception {
		if (sceneInfoDto != null) {
			Map<String, Object> session = ActionContext.getContext()
					.getSession();
			UserInfoDto user = (UserInfoDto) session.get("user");
			if (user != null) {
				sceneInfoDto.setUpdate_by(user.getUser_id().toString());
			}
			if (sceneInfoDto.getS_id() != null && sceneInfoDto.getS_id() > 0) {
				// 更新
				this.updateSceneInfo();
			} else {
				// 新建
				sceneInfoService.saveSceneInfo(sceneInfoDto);
				this.writeJSON("ok");
			}
		} else {
			this.writeJSON("no");
		}
	}

	/**
	 * 获取一个场景信息
	 * 
	 * @return
	 */
	public String getOneSceneInfo() {
		HttpServletRequest request = ServletActionContext.getRequest();
		Map<String, Object> param = new HashMap<String, Object>();
		String id = request.getParameter("id");
		if (StringUtils.isNotEmpty(id)) {
			param.put("s_id", id);
		}
		sceneInfoList = sceneInfoService.findOneSceneInfo(param);
		return SUCCESS;
	}

	/**
	 * 修改场景显示顺序
	 * 
	 * @throws Exception
	 */
	public void updateSceneOrder() throws Exception {
		System.out.println("进方法了");
		HttpServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("orderid");
		String order = request.getParameter("order");
		if (StringUtils.isNotBlank(id)) {
			Map<String, Object> session = ActionContext.getContext()
					.getSession();
			UserInfoDto user = (UserInfoDto) session.get("user");
			if (user != null && user.getUser_id() != null) {
				Map<String, Object> param = new HashMap<String, Object>();
				param.put("order", Integer.valueOf(order));
				param.put("s_id", Long.valueOf(id));
				param.put("update_by", user.getUser_id());
				sceneInfoService.updataSceneInfo(param);
			}
			this.writeJSON("ok");
		} else {
			this.writeJSON("no");
		}
	}

	/**
	 * 修改场景信息
	 * 
	 * @throws Exception
	 */
	public void updateSceneInfo() throws Exception {
		if (sceneInfoDto != null && sceneInfoDto.getS_id() != null) {
			Map<String, Object> session = ActionContext.getContext()
					.getSession();
			UserInfoDto user = (UserInfoDto) session.get("user");
			Map<String, Object> param = new HashMap<String, Object>();
			if (user.getUser_id() != null) {
				param.put("updata_by", sceneInfoDto.getUpdate_by());
			}
			if (sceneInfoDto.getType() > 0) {
				param.put("type", sceneInfoDto.getType());
			}
			if (sceneInfoDto.getDesc() != null) {
				param.put("desc", sceneInfoDto.getDesc());
			}
			if (sceneInfoDto.getTitle() != null) {
				param.put("title", sceneInfoDto.getTitle());
			}
			if (sceneInfoDto.getImage() != null) {
				param.put("image", sceneInfoDto.getImage());
			}
			if (sceneInfoDto.getUrl() != null) {
				param.put("url", sceneInfoDto.getUrl());
			}
			if (sceneInfoDto.getOrder() > 0) {
				param.put("order", sceneInfoDto.getOrder());
			}
			if (sceneInfoDto.getS_id() != null) {
				param.put("s_id", sceneInfoDto.getS_id());
			}
			sceneInfoService.updataSceneInfo(param);
			this.writeJSON("ok");
		} else {
			this.writeJSON("no");
		}
	}

	/**
	 * 删除场景信息
	 * 
	 * @throws Exception
	 */
	public void deleteSceneInfo() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		String ids = request.getParameter("ids");
		if (ids != null) {
			sceneInfoService.deleteSceneInfo(ids);
			this.writeJSON("ok");
		} else {
			this.writeJSON("no");
		}
	}

	public SceneInfoService getSceneInfoService() {
		return sceneInfoService;
	}

	public void setSceneInfoService(SceneInfoService sceneInfoService) {
		this.sceneInfoService = sceneInfoService;
	}

	public Map<String, Object> getMapSenceInfoResult() {
		return mapSenceInfoResult;
	}

	public List<SceneInfoDto> getSceneInfoList() {
		return sceneInfoList;
	}

	public void setSceneInfoList(List<SceneInfoDto> sceneInfoList) {
		this.sceneInfoList = sceneInfoList;
	}

	public SceneInfoDto getSceneInfoDto() {
		return sceneInfoDto;
	}

	public void setSceneInfoDto(SceneInfoDto sceneInfoDto) {
		this.sceneInfoDto = sceneInfoDto;
	}

	public void setMapSenceInfoResult(Map<String, Object> mapSenceInfoResult) {
		this.mapSenceInfoResult = mapSenceInfoResult;
	}

	public String getFindTitleName() {
		return findTitleName;
	}

	public void setFindTitleName(String findTitleName) {
		this.findTitleName = findTitleName;
	}

	public String getFindSceneType() {
		return findSceneType;
	}

	public void setFindSceneType(String findSceneType) {
		this.findSceneType = findSceneType;
	}
}
